<?php

	include("common.php");
	
	if(!empty($_POST)){
		if($_POST['autoprocess'] == "no"){
			$homeworks = $_POST['homework'];
		} else {
			$subject = $_POST['homework'];
			$homeworks = array();
			$i = 0;
			foreach(preg_split("/(\r?\n)/", $subject) as $line){
				if($i < 1){ $i++; continue; }
				if($i == "") continue;
				if(preg_match("/[HQO]/", substr($line, 0, 1))){
					$line = substr($line, 2, strlen($line));
				} elseif(preg_match("/    /", substr($line, 0, 4))){
					$line = substr($line, 4, strlen($line));
				}
				array_push($homeworks, $line);
				//echo $line . "<br>";
			}
		}
		mysql_query("DELETE FROM `197_homeworks` WHERE `date` = '" . $_POST['date'] . "'");
		foreach($homeworks as $row){
			mysql_query("INSERT INTO `197_homeworks` ( `content`, `date` ) VALUES ( '" . $row . "', '" . $_POST['date'] . "' )");
		}
		include("header.php");
		
?>
<h1>修改聯絡事項</h1>
<p>修改成功，請<a href="homeworkEdit.php?date=<?php echo $_POST['date']; ?>">按此</a>返回。</p>
<?php

		include("sidebar.php");
		include("footer.php");
		exit;
	}
	
	$result = mysql_query("SELECT * FROM `197_homeworks` WHERE `date` = '" . $_GET['date'] . "'");
	$homeworks = array();
	while($row = mysql_fetch_assoc($result)){
		array_push($homeworks, $row);
	}
	
?>
<?php include("header.php"); ?>
<div id="delete-homework-dialog"><p>確定刪除？</p></div>
<script type="text/javascript" defer="defer">
	$("#delete-homework-dialog").dialog({
		width: 300,
		height: 210,
		autoOpen: false,
		title: "刪除聯絡事項",
		modal: true,
		draggable: false,
		resizable: false
	});
	function addHomeworkItem(){
		$("#homework-list").append('<li><input type="text" size="50" name="homework[]" /></li>');
	}
	function showConfirm(hw_id){
		$("#delete-homework-dialog").dialog("option", "buttons", {"確定": function(){ $(this).dialog("close"); deleteHomework(hw_id); }, "取消": function(){ $(this).dialog("close"); }
		}).dialog("open");
	}
	function deleteHomework(hw_id){
		$("#delete-homework-dialog").dialog("option", "buttons", { "關閉": function(){ location.reload(); } });
		$("#delete-homework-dialog").html("<img src='../images/ajax-loader.gif' />");
		$("#delete-homework-dialog").dialog("open");
		$("#delete-homework-dialog").load("homeworkDelete.php?homework_id=" + hw_id + "&ajax=true");
	}
	$(function(){ $("#mode-tabs").tabs(); });
</script>
<h1><?php echo $_GET['date']; ?> 聯絡簿</h1>
<div id="mode-tabs">
	<ul>
		<li><a href="#normal-mode">普通模式</a></li>
		<li><a href="#auto-mode">自動模式</a></li>
	</ul>
	<div id="normal-mode">
		<form action="homeworkEdit.php" method="POST">
			<ul id="homework-list">
				<?php foreach($homeworks as $row){ ?>
					<li><input type="text" size="50" name="homework[]" value="<?php echo htmlspecialchars($row['content']); ?>" /> - [ <a href="homeworkDelete.php?homework_id=<?php echo $row['homework_id']; ?>" onclick="showConfirm(<?php echo $row['homework_id']; ?>); return false;">刪除</a> ]</li>
				<?php } ?>
			</ul>
			<input type="hidden" name="autoprocess" value="no" />
			<input type="submit" value="確定" />｜<a href="javascript:void(0)" onclick="addHomeworkItem();">新增</a><input type="hidden" name="date" value="<?php echo $_GET['date']; ?>" />
		</form>
	</div>
	<div id="auto-mode">
		<form action="homeworkEdit.php" method="POST">
			<p><textarea name="homework" rows="15" cols="50"></textarea></p>
			<input type="hidden" name="autoprocess" value="yes" />
			<p><input type="submit" value="確定" /><input type="hidden" name="date" value="<?php echo $_GET['date']; ?>" /></p>
		</form>
	</div>
</div>
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>